package com.chen.base.module.mybatis.methods;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.chen.base.utils.basic.StringUtil;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * 根据构造条件，查询单列结果集
 *
 * @author hubin
 * @since 2018-04-06
 */
public class SelectSinglyColumnList extends AbstractMethod {

    public SelectSinglyColumnList() {
        this(StringUtil.initialToLowerCase(SelectSinglyColumnList.class.getSimpleName()));
    }

    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public SelectSinglyColumnList(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sqlStr = "<script>select %s from %s %s\n</script>";
        String sql = String.format(sqlStr, sqlSelectColumns(tableInfo, true),
                tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo));
        
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForOther(mapperClass, methodName, sqlSource, Object.class);
    }

}